{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Coordinates and Other Geometric Manipulations\n",
    "\n",
    "Coordinates of `Molecule` objects are easy to manipulate "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports molli\n",
    "import molli as ml\n",
    "\n",
    "# Loads dendrobine to the s1 variable\n",
    "mol = ml.load(ml.files.dendrobine_mol2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Vectors, Angles and Dihedrals are also available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vector between Example Atom 1 and 2: [-1.2387  0.2093  0.8557]\n",
      "Angle between Example Atom 1, 2, and 3: 1.8082869758837117\n",
      "Angle between Example Atom 1, 2, and 3: -0.3286236550063439\n"
     ]
    }
   ],
   "source": [
    "#Gets an Example Atom 1 and 2\n",
    "ex_atom1, ex_atom2 = mol.get_atoms(0,1)\n",
    "\n",
    "#Finds vector between Example Atom 1 and 2\n",
    "v1 = mol.vector(ex_atom1, ex_atom2)\n",
    "print(f'Vector between Example Atom 1 and 2: {v1}')\n",
    "\n",
    "#Finds Angle between atoms 1, 2, and 3\n",
    "angle1 = mol.angle(0, 1, 2)\n",
    "print(f'Angle between Example Atom 1, 2, and 3: {angle1}')\n",
    "\n",
    "#Finds Dihedral Angle between atoms 1, 2, 3, and 4\n",
    "angle2 = mol.dihedral(0,1,2,3)\n",
    "print(f'Angle between Example Atom 1, 2, and 3: {angle2}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's very simple to translate individual atoms to set the origin of a molecule via translation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.0000e+00,  0.0000e+00,  0.0000e+00],\n",
       "       [-1.2387e+00,  2.0930e-01,  8.5570e-01],\n",
       "       [-2.3934e+00, -2.4190e-01, -6.1100e-02],\n",
       "       [-1.7244e+00, -1.7940e-01, -1.4357e+00],\n",
       "       [-4.2770e-01,  5.9170e-01, -1.2674e+00],\n",
       "       [ 1.1602e+00,  6.7360e-01,  5.6910e-01],\n",
       "       [-3.7288e+00,  4.9780e-01, -2.8380e-01],\n",
       "       [-3.9513e+00,  4.7910e-01, -1.8471e+00],\n",
       "       [-2.5388e+00,  7.9450e-01, -2.2833e+00],\n",
       "       [ 2.3930e-01,  8.1090e-01, -2.6849e+00],\n",
       "       [ 4.5000e-02,  2.2682e+00, -2.9177e+00],\n",
       "       [-1.2178e+00,  2.4610e+00, -3.4836e+00],\n",
       "       [-1.8937e+00,  1.0858e+00, -3.6690e+00],\n",
       "       [-1.7095e+00,  3.5499e+00, -3.7222e+00],\n",
       "       [-5.3580e-01,  3.9970e-01, -3.9671e+00],\n",
       "       [-5.1480e-01, -9.8220e-01, -4.6443e+00],\n",
       "       [-5.3400e-02,  1.0003e+00, -4.7652e+00],\n",
       "       [-2.4754e+00,  1.7899e+00, -1.8047e+00],\n",
       "       [-6.4440e-01,  1.6271e+00, -9.4960e-01],\n",
       "       [-2.6682e+00, -1.2591e+00,  2.4640e-01],\n",
       "       [-1.5788e+00, -1.6516e+00, -1.8627e+00],\n",
       "       [ 8.8150e-01, -1.6093e+00, -4.5885e+00],\n",
       "       [-9.4750e-01, -8.6870e-01, -6.1184e+00],\n",
       "       [-1.2370e+00, -1.6690e+00, -4.2148e+00],\n",
       "       [-1.3609e+00,  1.2583e+00,  1.1528e+00],\n",
       "       [-1.1798e+00, -4.0890e-01,  1.7574e+00],\n",
       "       [ 1.4190e+00,  2.2700e-01,  1.5349e+00],\n",
       "       [ 9.9670e-01,  1.7466e+00,  7.2040e-01],\n",
       "       [ 2.0295e+00,  5.4810e-01, -8.5200e-02],\n",
       "       [-3.6676e+00,  1.5281e+00,  8.6400e-02],\n",
       "       [-4.5599e+00,  5.8000e-03,  2.3120e-01],\n",
       "       [-4.6770e+00,  1.2388e+00, -2.1506e+00],\n",
       "       [-4.2894e+00, -5.0390e-01, -2.1892e+00],\n",
       "       [ 1.3128e+00,  6.1170e-01, -2.7064e+00],\n",
       "       [-2.5889e+00,  1.1091e+00, -4.5093e+00],\n",
       "       [-1.6035e+00, -2.3535e+00, -1.0195e+00],\n",
       "       [-6.0400e-01, -1.8571e+00, -2.2989e+00],\n",
       "       [-2.3903e+00, -1.9585e+00, -2.5284e+00],\n",
       "       [ 8.8350e-01, -2.5922e+00, -5.0719e+00],\n",
       "       [ 1.2216e+00, -1.7503e+00, -3.5584e+00],\n",
       "       [ 1.6183e+00, -9.8160e-01, -5.1013e+00],\n",
       "       [-9.4380e-01, -1.8540e+00, -6.5977e+00],\n",
       "       [-2.7280e-01, -2.2010e-01, -6.6880e+00],\n",
       "       [-1.9612e+00, -4.6480e-01, -6.2016e+00]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "#This gets the indeex of an atom\n",
    "ex_atom1_idx = mol.get_atom_index(ex_atom1)\n",
    "\n",
    "ml.CartesianGeometry\n",
    "#This places that atom at the origin\n",
    "mol.translate(-1*mol.coords[ex_atom1_idx])\n",
    "\n",
    "mol.coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`molli` also has the ability to rotate `Molecule` objects based on a pre-defined transformation matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
       "       [-6.55267747e-17, -2.55707541e-17,  1.52000219e+00],\n",
       "       [-1.44358231e+00, -4.02388288e-01,  1.88275494e+00],\n",
       "       [-2.17456623e+00, -1.03336554e-01,  5.72326391e-01],\n",
       "       [-1.23633091e+00,  7.28332315e-01, -2.83472867e-01],\n",
       "       [ 1.17936048e+00,  6.70362502e-01, -5.32352120e-01],\n",
       "       [-2.33993688e+00,  2.63127318e-01,  2.94749998e+00],\n",
       "       [-3.74322199e+00,  4.43941586e-01,  2.24617272e+00],\n",
       "       [-3.26373552e+00,  9.16990518e-01,  8.92946474e-01],\n",
       "       [-1.99217523e+00,  1.18794671e+00, -1.59484538e+00],\n",
       "       [-2.18900375e+00,  2.64567395e+00, -1.36689482e+00],\n",
       "       [-3.36255687e+00,  2.82339413e+00, -6.29828275e-01],\n",
       "       [-4.00108299e+00,  1.44187916e+00, -3.72748916e-01],\n",
       "       [-3.76159361e+00,  3.89663706e+00, -2.13509442e-01],\n",
       "       [-3.51235774e+00,  9.02641418e-01, -1.74163880e+00],\n",
       "       [-4.15136812e+00, -3.67738299e-01, -2.33027243e+00],\n",
       "       [-3.84221139e+00,  1.64048586e+00, -2.50135972e+00],\n",
       "       [-2.76578225e+00,  1.83896515e+00,  1.24777600e+00],\n",
       "       [-1.02762999e+00,  1.69185340e+00,  2.14603368e-01],\n",
       "       [-1.42405608e+00, -1.46931985e+00,  2.13974309e+00],\n",
       "       [-2.54450720e+00, -1.48842690e+00,  1.05705703e-02],\n",
       "       [-3.34791864e+00, -8.94665851e-01, -3.52310017e+00],\n",
       "       [-5.59335390e+00, -8.37018388e-02, -2.79188119e+00],\n",
       "       [-4.26599438e+00, -1.15719850e+00, -1.59450701e+00],\n",
       "       [ 2.47154314e-01,  9.86591138e-01,  1.93128667e+00],\n",
       "       [ 7.24313794e-01, -7.30633283e-01,  1.89449902e+00],\n",
       "       [ 2.08319231e+00,  1.14773108e-01, -2.61045853e-01],\n",
       "       [ 1.28566941e+00,  1.69777357e+00, -1.66185701e-01],\n",
       "       [ 1.13706592e+00,  6.98892325e-01, -1.62639894e+00],\n",
       "       [-1.92908187e+00,  1.23434700e+00,  3.24790316e+00],\n",
       "       [-2.43352793e+00, -3.53487700e-01,  3.84696807e+00],\n",
       "       [-4.35335433e+00,  1.18411441e+00,  2.77131989e+00],\n",
       "       [-4.28702409e+00, -5.04301451e-01,  2.19375675e+00],\n",
       "       [-1.40663797e+00,  1.07119654e+00, -2.50920890e+00],\n",
       "       [-5.08400155e+00,  1.53069099e+00, -2.76054174e-01],\n",
       "       [-1.92195390e+00, -2.29969165e+00,  4.08737403e-01],\n",
       "       [-2.35413645e+00, -1.56138388e+00, -1.05768595e+00],\n",
       "       [-3.57572850e+00, -1.75820115e+00,  2.54867185e-01],\n",
       "       [-3.81127519e+00, -1.79893573e+00, -3.93220731e+00],\n",
       "       [-2.32299092e+00, -1.15137945e+00, -3.23976940e+00],\n",
       "       [-3.29702311e+00, -1.51070311e-01, -4.32579606e+00],\n",
       "       [-6.05256389e+00, -9.90785111e-01, -3.20039606e+00],\n",
       "       [-5.62295918e+00,  6.83902839e-01, -3.57306798e+00],\n",
       "       [-6.21512762e+00,  2.53975370e-01, -1.95700594e+00]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Imports a the rotation matrix creation function\n",
    "from molli.math import rotation_matrix_from_vectors\n",
    "\n",
    "#This finds the vector between atom1 and atom2\n",
    "v1 = mol.vector(ex_atom1, ex_atom2)\n",
    "\n",
    "#This defines a transformation matrix to rotate v1 onto the z axis\n",
    "t_matrix = rotation_matrix_from_vectors(v1, [0,0,1])\n",
    "\n",
    "#This transforms the molecule to be on the z axis\n",
    "mol.transform(t_matrix)\n",
    "\n",
    "mol.coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Centroids are also available, along with distances to arbitrary points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here is the centroid\n",
      "[-2.49985446  0.30186006 -0.34948863]\n",
      "\n",
      "Here is the distance to the centroid from Atom 1: 2.5421514741871256\n"
     ]
    }
   ],
   "source": [
    "#This gives the array representing the molecule's centroid\n",
    "cent = mol.centroid()\n",
    "\n",
    "#This gives the distance of Atom 1 to the centroid\n",
    "dist_to_cent = mol.distance_to_point(ex_atom1, cent)\n",
    "\n",
    "print(f'Here is the centroid\\n{cent}\\n')\n",
    "print(f'Here is the distance to the centroid from Atom 1: {dist_to_cent}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dev-blake",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
